const db = require('../utils/settings');

function Blog() {

}

//删除操作
Blog.prototype.delBlog = function (req, res, next) {
    db.query('DELETE FROM blog_list_table WHERE id="'+req.query.id+'"',function (err,resultData) {
        if(err){
            console.error(err);
            res.status(500).send({code:500,msg:'database error'});
        }else{
            res.redirect('/admin/blog');
        }
    });
}

Blog.prototype.modBlog = function (req, res, next) {
    db.query('SELECT * FROM blog_list_table WHERE id="'+req.query.id+'"',function (err,modData) {
        if(err){
            console.error(err);
            res.status(500).send({code:500,msg:'database error'});
        }else if(modData.length == 0){
            res.status(400).send({code:400,msg:'parameters error'});
        }else{
            db.query('SELECT * FROM blog_list_table',function (err,allData) {
                if(err){
                    console.error(err);
                    res.status(500).send({code:500,msg:'database error'});
                }else{
                    res.render('blog',{formData:allData,modData:modData});
                }
            });
        }
    });
}

Blog.prototype.searchBlog = function (req, res, next) {
    db.query('SELECT * FROM blog_list_table', function (err, resultData) {
        if (err) {
            console.error(err);
            res.status(500).send({code: 500, msg: 'database error'}).end();
        } else {
            res.render('blog', {formData: resultData});
        }
    });
}

Blog.prototype.modified = function (req, res, next) {
    //此处验证应该更加严格，比如正则
    var title = req.body.title.trim();
    var author = req.body.author.trim();
    var summary = req.body.summary.trim();
    var href = req.body.href.trim();
    if (title && author && summary && href) {
        db.query('UPDATE blog_list_table SET title="'+title+'",author="'+author+'",summary="'+summary+'",href="'+href+'" WHERE ID="'+req.body.modified+'"',function (err,resultData) {
            if(err){
                console.error(err);
                res.status(500).send({code:500,msg:'database error'});
            }else{
                res.redirect('/admin/blog');
            }
        })
    } else {
        res.status(400).send({code: 400, msg: 'parameters error'}).end();
    }
}

Blog.prototype.addBlog = function (req, res, next) {
    //此处验证应该更加严格，比如正则
    var title = req.body.title.trim();
    var author = req.body.author.trim();
    var summary = req.body.summary.trim();
    var href = req.body.href.trim();
    if (title && author && summary && href) {
        db.query('INSERT INTO blog_list_table (title,author,summary,href) VALUE("' + title + '","' + author + '","' + summary + '","' + href + '")', function (err, data) {
            if (err) {
                console.error(err);
                res.status(500).send({code: 500, msg: 'database error'}).end();
            } else {
                res.redirect('/admin/blog');
            }
        });
    } else {
        res.status(400).send({code: 400, msg: 'parameters error'}).end();
    }
}

module.exports = new Blog();